# -*- coding: utf-8 -*-
# running with python 2.7 and PIL

# TODO: adding title retriving

import urllib2

#PNUM = 287039
#PNUM = 376587 # bambook
PNUM = 157698 # D10
#PNUM = 325250
PIMG = "gp%d,3.png"%PNUM
PURL = "http://price.360buyimg.com/%s"%PIMG

PROD_URL = "http://www.360buy.com/product/%d.html"%PNUM

f = urllib2.urlopen(PURL)

fi = file(PIMG, 'wb')
fi.write(f.read())
fi.close()
f.close()

# title
from page_grab import *
f = urllib2.urlopen(PROD_URL)
hp = PG()
hp.feed(f.read())
f.close()
hp.close()
print hp.rt()


from cell_grab import *

#http://www.360buy.com/product/376587.html
#http://price.360buyimg.com/gp376587,3.png

#http://www.360buy.com/product/287039.html
#http://price.360buyimg.com/gp287039,3.png

# price pic address never change, but server regenerates it


#lambda_table = (
#    1 : 63.85815484985084,
#    2 : 82.31187740100451,
#    3 : 89.20091035783956,
#    4 : 82.03650440838175,
#    5 : 89.12817506949783,
#    6 : 98.26517025524818,
#    7 : 60.708500494024605,
#    8 : 105.50494342050213,
#    9 : 94.92290729236929,
#    0 : 103.20235832750811
#)

#Yen: 104.10733553135425
#dot: 15.824113776358011

lambda_table = {
    10410733 : 'RMB ',
    1582411 : '.',
    6385815 : 1,
    8231187 : 2,
    8920091 : 3,
    8203650 : 4,
    8912817 : 5,
    9826517 : 6,
    6070850 : 7,
    10550494 : 8,
    9492290 : 9,
    10320235 : 0
}

gc = GrabCell()
#fl = ("gp233326,3.png",
#      "gp376587,3.png",
#      "gp495084,3.png",
#      "gp509021,3.png",
#      "gp1000396784,3.png")

fl = (PIMG, )

for x in fl:
  #print "\n\n\n\n"
  #print "file: ", x
  ret = gc.action(x)
  p = ''
  for y in ret:
    ldv = math.floor(y*100000)  # cut tail
    #print ldv
    p += str(lambda_table[ldv])
  print 'product', PNUM, ':', p

